package com.jinlia.show.modules.wxapp.websocket;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.adapter.standard.StandardWebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

import javax.websocket.Session;

@Component
@Slf4j
public class WebSocketHandler extends AbstractWebSocketHandler {

    @Autowired
    WebSocketService webSocketService;

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        Session nativeSession = ((StandardWebSocketSession) session).getNativeSession();
        String userId = nativeSession.getQueryString();
        log.info("建立websocket连接={}", userId);
        WebSocketSessionManager.add(userId, session);
    }


    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
        // socket连接关闭后触发
        Session nativeSession = ((StandardWebSocketSession) session).getNativeSession();
        String userId = nativeSession.getQueryString();
        log.info("关闭websocket连接={}", userId);
        WebSocketSessionManager.removeAndClose(userId,session);
    }
}
